package com.shiguiwu.springboot3.rabbitmq.listener;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.shiguiwu.springboot3.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * @description: 监听消息
 * @author: stone
 * @date: Created by 2023/11/17 21:35
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.rabbitmq.listener
 */
@Component
@Slf4j
@RabbitListener(queues = "queue_topic2")
public class RabbitMQ2Listener {

    @RabbitHandler
    public void handleTest(UserDTO userDTO, Message message, Channel channel) throws IOException {
        log.error("第二个消费者：{}", JSONUtil.toJsonStr(userDTO));

        try {

            log.info("消费消息业务逻辑。。。。。。。。。。。。。");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("消费消息报错{}", userDTO.getId(), e);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
